<?php

	class WebpayController extends AppController
	{
		var $name = 'Webpay';
		var $uses = null;
		
		var $publicAction = array('create_dm');
		
		var $accountResellerDM = '192894'; // Ricardo Gatica
		var $urlDMResellerStep1 = 'https://chile.dineromail.com/DM_PDC/landing_res.asp?r=12902180914028';
		var $urlDMResellerStep2 = 'https://chile.dineromail.com/registracion/RI.asp';
		var $urlDMResellerStep3 = 'https://cl.dineromail.com/Register/Create';
		
		function create_dm()
		{
			$this->autoRender = false;
			
			set_time_limit(300); // 5 Min. PHP
			
			$userAgent = 'Mozilla/5.0 (Windows; U; Windows NT 6.0; es-ES; rv:1.9.0.6) Gecko/2009011913 Firefox/3.0.6';
			
			// Step 1
			$ckfile = tempnam (ROOT . DS . 'tmp', 'CURLCOOKIE');
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $this->urlDMResellerStep1);
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_COOKIEJAR, $ckfile);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
			$result = curl_exec($ch);
			curl_close($ch);
			
			// Step 2
			$ch = curl_init();
			curl_setopt($ch, CURLOPT_URL, $this->urlDMResellerStep2);
			curl_setopt($ch, CURLOPT_FAILONERROR, true); 
			curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // allow redirects
			curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
			curl_setopt($ch, CURLOPT_HEADER, false);
			curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
			curl_setopt($ch, CURLOPT_BINARYTRANSFER, false);
			curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300); // 5 Min.cURL
			curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
			$result = curl_exec($ch);
			curl_close($ch);
			
			preg_match_all('/value = "(.*)"/U', $result, $matches, PREG_SET_ORDER);
			
			if (!empty($matches[0][1]) && !empty($matches[1][1])) {
				// Step 3
				$ch = curl_init();
				curl_setopt($ch, CURLOPT_URL, $this->urlDMResellerStep3);
				curl_setopt($ch, CURLOPT_FAILONERROR, true); 
				curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); // allow redirects
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($ch, CURLOPT_HEADER, false);
				curl_setopt($ch, CURLOPT_COOKIEFILE, $ckfile);
				curl_setopt($ch, CURLOPT_BINARYTRANSFER, false);
				curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
				curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 300); // 5 Min.cURL
				curl_setopt($ch, CURLOPT_USERAGENT, $userAgent);
				
				curl_setopt($ch, CURLOPT_HTTPHEADER, Array("Content-Type: application/x-www-form-urlencoded"));
				curl_setopt($ch, CURLOPT_POST, 1); // set POST method
				curl_setopt($ch, CURLOPT_POSTFIELDS, "Code={$matches[0][1]}&TypeReg={$matches[1][1]}"); // add POST fields
				$result = curl_exec($ch);
				curl_close($ch);
				
				$result = preg_replace(array('/<title>(.*)<\/title>/s'), array('<title>Plasa - DineroMail - Vende y Compra</title>'), $result);
				$result = preg_replace(array('/<div id="Footer">(.*)<\/[^>]+>/s'), array(''), $result);
				$result = preg_replace(array('/href="\//s','/src="\//'), array('href="https://cl.dineromail.com/','src="https://cl.dineromail.com/'), $result);
				
				$html = $result;
				
				$dom = new DOMDocument();
				@$dom->loadHTML($html);
				
				$xpath = new DOMXPath($dom);
				
				echo $result;
				
				
				
			}
			else {
				
			}
			
		}
	}
